package edu.hawaii.duedates.configuration;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import edu.hawaii.duedates.configuration.jaxb.DueDates;


/**
 * Provides access to the contents of the "default" stack.
 * The default stack is stored in ~/.stack/stack.xml
 * @author Philip Johnson
 */
public class ConfigurationManager {
  /** The JAXB Stack instance read in from the XML file. */
  private DueDates duedate;

  /**
   * Creates a new ConfigurationManager that reads in the XML stack representation.
   * @param configFile An XML file containing a stack representation.
   */
  public ConfigurationManager(File configFile) {
    try {
      JAXBContext jaxbContext = 
        JAXBContext.newInstance(edu.hawaii.duedates.configuration.jaxb.ObjectFactory.class);
      if (configFile.exists()) {
        // Left in for teaching purposes. Don't use System.out.println in production code! 
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        this.duedate = (DueDates) unmarshaller.unmarshal(configFile);
        //List<Users> user = this.duedate.getUsers();
      }
    }
    catch (Exception e) {
      throw new RuntimeException("Error in Stack ConfigurationManager", e);
    }
  }
  
  /**
   * Create a new ConfigurationManager that provides access to the stack in ~/.stack/stack.xml.
   */
  public ConfigurationManager() {
    this(new File(System.getProperty("user.home") + "/.duedates/duedates.xml"));
  }
  
  /**
   * Returns a Stack constructed from the default XML stack. 
   * If no default Stack exists, an empty stack is returned. 
   * @return The Stack instance constructed from the XML stack. 
   */
  public DueDates getDueDates() {
    return this.duedate;
  }
}
